/******************************************************************************
 Copyright (c) 2019-2025 Qualcomm Technologies International, Ltd.
 All Rights Reserved.
 Qualcomm Technologies International, Ltd. Confidential and Proprietary.

******************************************************************************/
#ifndef __CSR_BT_TD_DB_PS_H_
#define __CSR_BT_TD_DB_PS_H_


#ifdef __cplusplus
extern "C" {
#endif

#include "csr_synergy.h"
#include "csr_bt_td_db.h"
#include "td_db_main.h"
#include "csr_log_text_2.h"
#ifdef INCLUDE_BT_WEARABLE_TD_DB_PS
#include "qapi_ps.h"

#ifndef SYNERGY_PS_BASE
#define SYNERGY_PS_BASE_MASK            0x20000000
#define SYNERGY_PS_BASE                 1
#endif /* !SYNERGY_PS_BASE */


#else

#ifndef SYNERGY_PS_BASE
#define SYNERGY_PS_BASE                 100
#endif /* !SYNERGY_PS_BASE */

#endif

CSR_LOG_TEXT_HANDLE_DECLARE(TdDbPsLto);

/* Rank is 4 bits so 0xF is used as invalid value */
#define TD_DB_DEVICE_RANK_INVALID       (0xF)
#define TD_DB_INVALID_VERSION           (0x0)

/* Common keys */
#define CSR_BT_PS_KEY_SYSTEM            SYNERGY_PS_BASE
#define CSR_BT_PS_KEY_INDEX             (CSR_BT_PS_KEY_SYSTEM + 1)

/* Source bases */
#define CSR_BT_TD_DB_BASE               (CSR_BT_PS_KEY_INDEX + 1)
#define CSR_BT_TD_DB_BASE_GAP           (CSR_BT_TD_DB_BASE)
#define CSR_BT_TD_DB_BASE_SC            (CSR_BT_TD_DB_BASE_GAP + (CSR_BT_TD_DB_KEYCOUNT_GAP * CSR_BT_TD_DB_LIST_MAX))
#define CSR_BT_TD_DB_BASE_GATT          (CSR_BT_TD_DB_BASE_SC + (CSR_BT_TD_DB_KEYCOUNT_SC * CSR_BT_TD_DB_LIST_MAX))
#define CSR_BT_TD_DB_BASE_DIRECT        (CSR_BT_TD_DB_BASE_GATT + (CSR_BT_TD_DB_KEYCOUNT_GATT * CSR_BT_TD_DB_LIST_MAX))

#define SYNERGY_PS_BASE_TD_MAX          (CSR_BT_TD_DB_BASE_DIRECT + (CSR_BT_TD_DB_KEYCOUNT_DIRECT * CSR_BT_TD_DB_LIST_MAX) - 1)

/* Total of 50 keys are allocated for TD DB usage */
#define SYNERGY_PS_NUM_KEYS             (50)
#define SYNERGY_PS_BASE_TD_RANGE_END    (SYNERGY_PS_BASE + SYNERGY_PS_NUM_KEYS - 1)

/* Stores meta information regarding all the present devices in the PS.*/
#ifdef INSTALL_EXTENDED_TD_DB
typedef struct
{
    CsrUint16               deviceAddress[3];   /* Bluetooth device address of the trusted device.*/
    CsrBtAddressType        addressType:2;      /* Address type of the stored device. See CsrBtAddressType for more information.*/
    CsrUint8                rank:4;             /* The rank of the bluetooth device. Lower the rank, more recently used the
                                                 * device is. e.g. rank=0 is a Most Recently Used (MRU) device. */
    CsrBool                 priorityDevice:1;   /* Whether a device is priority or not. Priority devices get special
                                                 * treatment such as you cannot delete them using CsrBtTdDbDeleteDevice API. */
#ifdef TD_DB_INSTALL_OPTIMIZED_PS_ACCESS
    CsrUint8                keyMask:8;          /* Bits representing the TDDB keys which are written in the PS. Used to efficiently manage PS. */
#endif /* TD_DB_INSTALL_OPTIMIZED_PS_ACCESS */
} TdDbIndexElem;
#else
typedef struct
{
    CsrBtDeviceAddr         deviceAddress;  /* Bluetooth device address of the trusted device.*/
    CsrBtAddressType        addressType;    /* Address type of the stored device. See CsrBtAddressType for more information.*/
    CsrUint8                rank;           /* The rank of the bluetooth device. lower the rank more recently used the
                                             * device is. e.g. Rank=0 is a Most Recently Used (MRU) device. */
    CsrBool                 priorityDevice; /* Whether a device is priority or not. Priority devices get special
                                             * treatment such as you cannot delete them using CsrBtTdDbDeleteDevice API. */
#ifdef TD_DB_INSTALL_OPTIMIZED_PS_ACCESS
    CsrUint8                keyMask;        /* Bits representing the logical TDDB keys which are written in the PS. Used to efficiently manage PS. */
#endif /* TD_DB_INSTALL_OPTIMIZED_PS_ACCESS */
} TdDbIndexElem;
#endif

/* This key stores the meta information (limited) of all the devices which are present in the PS. This can be
 * considered as an index into the database. Frequently used information are stored here in order to get these
 * information for all the devices in one PS read.
 * This structure is stored on key 101 (CONNLIB01).
 */
typedef struct
{
    CsrUint16               count;                          /* Number of devices which are present in the PS.*/
    TdDbIndexElem           device[CSR_BT_TD_DB_LIST_MAX];  /* Contains information regarding stored devices. 
                                                             * See TdDbIndexElem for more information.*/
} TdDbIndex;

/* This is stored on PS key 100 (CONNLIB0). Contains the system level information which is common for all the devices
 * which are present in the database. */
typedef struct
{
    CsrUint32 version;                  /* Indicates the kind of features which are enabled in the PS. As and when any
                                         * major feature is updated or added, this value will change. This is indicated
                                         * by CSR_BT_TD_DB_FILE_VERSION, please refer to td_db_main.h file
                                         * for more information. */
    CsrBtTdDbSystemInfo systemInfo;     /* Please refer to CsrBtTdDbSystemInfo in csr_bt_td_db.h for more information.*/
} TdDbSystemInfo;

/* Trusted database cache stored in RAM. This is used to avoid reading PS for operations which requires meta
 * information of the device such as bd address, rank etc.*/
typedef struct
{
    CsrUint24   lap:24;             /* Lower address part of the device address.*/
    CsrUint8    uap:8;              /* upper address part of the device address.*/
    CsrUint16   nap:16;             /* Non significant addrss part of the device address.*/
    CsrUint8    rank: 4;            /* Rank of the device. Lower the rank, more recent the device is.
                                     * E.g. rank=0 is Most Recently Used (MRU) */
    CsrUint8    addressType: 1;     /* Type of the bluetooth device address. */
    CsrUint8    priorityDevice: 1;  /* Whether a device is priority or not. Priority devices get special
                                     * treatment such as you cannot delete them using CsrBtTdDbDeleteDevice API. */
#ifdef TD_DB_INSTALL_OPTIMIZED_PS_ACCESS
    CsrUint8    keyMask: 8;         /* Bits representing the logical TDDB keys which are written in the PS. Used to efficiently manage PS.*/
#endif /* TD_DB_INSTALL_OPTIMIZED_PS_ACCESS */
} TdDbCacheInfo;

extern CsrSize tdDbMapSize;
extern const CsrUint8 tdDbMap[];

CSR_COMPILE_TIME_ASSERT(SYNERGY_PS_BASE_TD_MAX <= SYNERGY_PS_BASE_TD_RANGE_END, \
                        CSR_SYNERGY_PS_KEY_OVERFLOW);

CSR_BT_TD_DB_KEY_STRUCT_COMPILE_ASSERT(TdDbIndex);

#ifdef INCLUDE_BT_WEARABLE_TD_DB_PS
#define PsStore(Key, buff, words)                         PsStoreWearable(Key, buff, words, __LINE__)
#define PsRetrieve(Key, buff, words)                      PsRetrieveWearable(Key, buff, words, __LINE__)
#else
extern CsrUint16 PsStore(CsrUint16 key, const void *buff, CsrUint16 words);
extern CsrUint16 PsRetrieve(CsrUint16 key, void *buff, CsrUint16 words);
#endif
#define CSR_BT_TD_DB_SIZE_IN_WORDS(_struct)     (sizeof(_struct) / CSR_BT_TD_DB_WORD_SIZE)

CsrBtResultCode TdDbPsFindPsKey(CsrUint8 deviceIndex,
                                CsrBtTdDbSource source,
                                CsrUint16 key,
                                CsrUint16 *psKey,
                                CsrUint8 mapSize,
                                const CsrUint8 *map,
                                CsrUint8 listMax,
                                CsrUint16 keyMax);

CsrBool TdDbPsUpdateIndexData(TdDbIndex *tdDbIndex);

#ifdef INSTALL_EXTENDED_TD_DB
CsrBool TdDbPsExtendedRestructurePsData(CsrUint32 version, CsrBtTdDbSystemInfo *systemInfo);
#endif

#ifdef __cplusplus
}
#endif

#endif /* __CSR_BT_TD_DB_PS_H_ */
